package com.sgcc.fj.pmos.mobileapp.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.arouter.app.AppArouterPath;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.check.EasyProtectorLib;
import com.sgcc.fj.pmos.common.utils.ArguConstant;
import com.sgcc.fj.pmos.common.utils.ConfigUtil;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.fj.pmos.common.utils.UpdateAppUtil;
import com.sgcc.fj.pmos.e_fj_home.fragment.HomeFragment;
import com.sgcc.fj.pmos.e_fj_mall.fragment.MallFragment;
import com.sgcc.fj.pmos.e_fj_order.fragment.OrderFragment;
import com.sgcc.fj.pmos.e_fj_personalcenter.fragment.UserFragment;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.mobileapp.widget.BottomBar;

import org.greenrobot.eventbus.EventBus;

@Route(path = AppArouterPath.APP_MAIN)
public class MainActivity extends MvpBaseActivity {

    private static final String TAG = "MainActivity";
    private long exitTime = 0;
    private BottomBar mBottomBar;
    private View mVStatusBar;
    private boolean mIsRelease;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mVStatusBar = (View) findViewById(R.id.view_status_bar);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, BarUtils.getStatusBarHeight());
        mVStatusBar.setLayoutParams(params);

        mBottomBar = (BottomBar) findViewById(R.id.bottom_bar);
        mBottomBar.setContainer(R.id.fl_container)
                .setTitleSize(13)
                .setTitleBeforeAndAfterColor(getResources().getColor(R.color._999999), getResources().getColor(R.color._3274F0))
                .addItem(HomeFragment.class, "首页", R.mipmap.un_home, R.mipmap.home)
                .addItem(MallFragment.class, "商城", R.mipmap.un_mall, R.mipmap.mall)
                .addItem(OrderFragment.class, "合约", R.mipmap.un_order, R.mipmap.order)
                .addItem(UserFragment.class, "我的", R.mipmap.un_user, R.mipmap.user)
                .setFirstChecked(0).build();

        TextView tvText = findViewById(R.id.tvText);
        mIsRelease = SPUtils.getInstance("environment").getBoolean("ISRELEASE", false);
        tvText.setText(mIsRelease ? "生产" : "测试");
        tvText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIsRelease = SPUtils.getInstance("environment").getBoolean("ISRELEASE", false);
                SPUtils.getInstance("environment").put("ISRELEASE", !mIsRelease, true);
                tvText.setText(!mIsRelease ? "生产" : "测试");

                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            }
        });
        boolean isFirstStart = SPUtils.getInstance("isFirstStart").getBoolean("isFirstStart", true);
        if (isFirstStart)
            DialogUtil.showFirstTipsDialog(mContext);
    }

    @Override
    protected void onStart() {
        super.onStart();
        UpdateAppUtil.update(mContext);
    }

    @Override
    public void onEventCallBack(EventCenter eventCenter) {
        super.onEventCallBack(eventCenter);
        switch (eventCenter.getEventCode()) {
            case ArguConstant.GO_SIGNING:
                mBottomBar.switchPage(1);
                break;
            case ArguConstant.GO_ORDER:
                mBottomBar.switchPage(2);
                String jumpType = SPUtils.getInstance().getString("order_success", "");
                if (!TextUtils.isEmpty(jumpType)) {
                    if (TextUtils.equals("order", jumpType)) {
                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.GO_ORDER_LIST_FRFRESH));
                    } else if (TextUtils.equals("contract", jumpType)) {
                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.GO_CONTRACT_FRFRESH));
                    }
                    SPUtils.getInstance().remove("order_success");
                } else {
                    EventBus.getDefault().post(new EventCenter<>(ArguConstant.GO_CONTRACT));
                }
                break;
            case ArguConstant.INTERFACE_LOGOUT:
                showLogoutDialog();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtils.showShort("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!AppUtils.isAppDebug()) {
            if (ConfigUtil.CAN_DEBUG_EMULATOR) {
                if (EasyProtectorLib.checkIsRoot()) {
                    showErrorDialog();
                }

            }
        }
    }

}
